using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AnimatorStateController : MonoBehaviour
{
    public delegate void AnimatorStateEvent(Animator animator, AnimatorStateInfo stateInfo, int layerIndex);
    private event AnimatorStateEvent m_EventEnter;
    private event AnimatorStateEvent m_EventExit;


    public delegate void AnimatorTriggerEvent(Animator animator, AnimatorStateInfo stateInfo, string name);
    private event AnimatorTriggerEvent m_EventEnd;

    Animator m_Animtor;
    private void Awake()
    {
        m_Animtor = GetComponent<Animator>();
    }

    public AnimatorStateInfo DefaultAnimatorStateInfo;
    public AnimatorStateInfo CurAnimatorStateInfo
    {
        get;
        private set;
    }

    public void SubscribeEnter(AnimatorStateEvent handler)
    {
        m_EventEnter -= handler;
        m_EventEnter += handler;
    }

    public void UnsubscribeEnter(AnimatorStateEvent handler)
    {
        m_EventEnter -= handler;
    }


    public void SubscribeExit(AnimatorStateEvent handler)
    {
        m_EventExit -= handler;
        m_EventExit += handler;
    }

    public void UnsubscribeExit(AnimatorStateEvent handler)
    {
        m_EventExit -= handler;
    }

    public void StateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        CurAnimatorStateInfo = stateInfo;
        if (null != m_EventEnter)
            m_EventEnter(animator, stateInfo, layerIndex);
    }
    public void StateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        if (null != m_EventExit)
            m_EventExit(animator, stateInfo, layerIndex);
    }

    public void Clear()
    {
        m_EventEnter = null;
        m_EventExit = null;
        m_EventEnd = null;
        CurAnimatorStateInfo = DefaultAnimatorStateInfo;
    }

    public void SubscribeEnd(AnimatorTriggerEvent handler)
    {
        m_EventEnd -= handler;
        m_EventEnd += handler;
    }

    public void UnsubscribeEnd(AnimatorTriggerEvent handler)
    {
        m_EventEnd -= handler;
    }

    public void StateEnd(string name)
    {
        if(m_EventEnd != null)
        {
            m_EventEnd(m_Animtor, m_Animtor.GetCurrentAnimatorStateInfo(0), name);
        }
    }
}